package com.android.topview.watermelon.exchange

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.android.topview.watermelon.network.NetworkGetter
import java.net.SocketException

class ExchangeResultViewModel : ViewModel() {

    internal val exchangeRate = MutableLiveData<Triple<ExchangeCode, ExchangeCode, Double>>(null)

    internal fun getExchangeRate(
        from: ExchangeCode,
        to: ExchangeCode
    ) {
        val rate = try {
            NetworkGetter.getExchangeRate(from, to)
        } catch (e: SocketException) {
            -2.0
        } catch (e: Exception) {
            -3.0
        }
        exchangeRate.postValue(Triple(from, to, rate))
    }

}